package com.molyfun.parents.activity.ask.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.TagListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/31/16.
 *
 * 标签展示
 */
public class TagView extends LinearLayout implements FlowTagLayout.OnTagSelectListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.flow_tag_layout)
    FlowTagLayout mFlowTagLayout;

    private List<String> mTagList;

    public TagView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public TagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TagView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_tag, this);
        ButterKnife.inject(this);
        mTagList = new ArrayList<>();
        initData();
    }

    /**
     * init data
     * and associated to TopicDetailBean
     */
    public void initData() {

        Call<TagListResponse> call = MFPApplication.getInstance().getRestClient().getTagList();
        call.enqueue(new Callback<TagListResponse>() {
            @Override
            public void onResponse(Call<TagListResponse> call, Response<TagListResponse> response) {
                if (response.body() != null && response.body().getData() != null && response.body().getData().getRecords() != null) {
                    mTagList.clear();
                    for (TagListResponse.TagBean tagBean : response.body().getData().getRecords()) {
                        mTagList.add(tagBean.getName());
                    }
                    mTagList.add("其他");
                    mFlowTagLayout.initData(mTagList);
                    mFlowTagLayout.setOnTagSelectListener(TagView.this);
                }
            }

            @Override
            public void onFailure(Call<TagListResponse> call, Throwable t) {
            }
        });
    }

    public void setOnTagViewCallback(OnTagViewCallback onTagViewCallback){
        this.mOnTagViewCallback = onTagViewCallback;
    }

    public void addTag(String tag){
        if(!TextUtils.isEmpty(tag)){
            mTagList.remove(mTagList.size() - 1);
            mTagList.add(tag);
            mTagList.add("其他");
            mFlowTagLayout.initData(mTagList, true, FlowTagLayout.TagType.TagTypeTopic);
        }
    }

    /**
     * 获取选中的tags json
     * @return
     */
    public List<TagListResponse.TagBean> getSelectedTags(){
        List<TagListResponse.TagBean> mSelectedTag = new ArrayList<>();
        if(mSelectedList != null){
            for(Integer index : mSelectedList){
                if(index < mTagList.size()){
                    TagListResponse.TagBean tagBean = new TagListResponse.TagBean();
                    tagBean.setName(mTagList.get(index));
                    mSelectedTag.add(tagBean);
                }
            }
        }
        return mSelectedTag;
    }

    private List<Integer> mSelectedList;
    @Override
    public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList, int position, boolean selectedState) {
        mSelectedList = selectedList;
        if(selectedState){
            if(position == mTagList.size() - 1){
                if(mOnTagViewCallback != null){
                    mOnTagViewCallback.onAddTag();
                }
            }
        }else{
            if(position == mTagList.size() - 1){
                if(mOnTagViewCallback != null){
                    mOnTagViewCallback.onCancelAddTag();
                }
            }
        }
    }

    private OnTagViewCallback mOnTagViewCallback;

    public static interface OnTagViewCallback{
        public void onAddTag();
        public void onCancelAddTag();
    }

}